unit Uadministracao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset;

type
  TfrmAdministracao = class(TForm)
    Label1: TLabel;
    lb_codigo: TLabel;
    Label3: TLabel;
    ed_operador: TEdit;
    Label4: TLabel;
    ed_caixa_programa: TEdit;
    Label5: TLabel;
    ed_notas: TEdit;
    Label6: TLabel;
    ed_moedas: TEdit;
    Label7: TLabel;
    ed_data: TEdit;
    lb_diferenca: TLabel;
    Label8: TLabel;
    lb_total: TLabel;
    Label11: TLabel;
    Button1: TButton;
    Button2: TButton;
    bt_salvar: TSpeedButton;
    bt_novo: TSpeedButton;
    bt_apagar: TSpeedButton;
    resultados: TStringGrid;
    Label12: TLabel;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
   procedure existe(codigo:word; var existe:boolean; var data:string);
  end;

var
  frmAdministracao: TfrmAdministracao;

implementation

uses Udm;

{$R *.dfm}

procedure AutoSizeGrid(Grid: TStringGrid);
const
  ColWidthMin = 10;
var
  C, R, W, ColWidthMax: integer;
begin
  for C := 0 to Grid.ColCount - 1 do begin
    ColWidthMax := ColWidthMin;
    for R := 0 to (Grid.RowCount - 1) do begin
      W := Grid.Canvas.TextWidth(Grid.Cells[C, R]);
      if W > ColWidthMax then
        ColWidthMax := W;
    end;
    Grid.ColWidths[C] := ColWidthMax + 5;
  end;
end;

procedure TfrmAdministracao.FormShow(Sender: TObject);
var sql:tzquery;
    exist:boolean;
    data:string;
    I,i2:word;
begin
 sql := tzquery.create(Application);
 sql.Connection := dm.conexao;
 sql.sql.clear;
 sql.Sql.Add('SELECT *, (notas + moedas) - caixa_programa  AS diferenca, moedas + notas as total');
 sql.Sql.Add('FROM movimento order by data desc LIMIT 1');
 sql.open;
  lb_codigo.Caption := sql.fieldbyname('codigo').asString;
  ed_operador.Text := sql.fieldbyname('operador').asString;
  ed_caixa_programa.Text := sql.fieldbyname('caixa_programa').asString;
  ed_notas.Text := sql.fieldbyname('notas').asString;
  ed_moedas.Text := sql.fieldbyname('moedas').asString;
   ShortDateFormat := 'yyyy-mm-dd';
    ed_data.Text := sql.fieldbyname('data').asString;
   ShortDateFormat := 'dd/mm/yyyy';
  lb_diferenca.Caption := sql.fieldbyname('diferenca').asString;
  lb_total.Caption := sql.fieldbyname('total').asString;
 sql.close;

 existe(strtoint(lb_codigo.caption)-1,exist,data);
 if exist then begin
  sql.SQL.Clear;
  sql.sql.add('SELECT * FROM( SELECT operador,cliente,acao,maquina,valor,tarifa, concat(data," ",hora) as data FROM log WHERE');
  sql.sql.add('((data >= date("'+data+'")) and (data <= date("'+ed_data.text+'")))) as tb');
  sql.sql.add('WHERE (data >= "'+data+'" and data <= "'+ed_data.text+'")');
//  sql.sql.add('(acao <> "LOGIN") and');
//  sql.sql.add('(acao <> "LOGOFF")');
//  sql.sql.add('ORDER by data asc');
  showmessage(sql.sql.Text);
  sql.Open;
  resultados.RowCount := sql.RecordCount+1;
  resultados.ColCount := sql.fields.Count;
  for i := 0 to sql.fields.Count -1 do resultados.Cells[i,0] := sql.fields[i].FieldName;
   if sql.RecordCount > 0 then begin
    for i := 1 to sql.RecordCount do begin
     for i2 := 0 to sql.Fields.Count -1 do resultados.Cells[i2,i] := sql.fields[i2].asString;
     sql.Next;
    end;
   end;
  sql.Close;
  autosizegrid(resultados);
  resultados.Visible := true;
 end else  resultados.Visible := false;

 sql.free;

end;

procedure TfrmAdministracao.existe(codigo:word; var existe: boolean; var data: string);
var sql:tzquery;
begin
 existe := false;
 sql := tzquery.create(Application);
 sql.Connection := dm.conexao;
 sql.sql.clear;
 sql.sql.add('SELECT data FROM movimento where codigo='+inttostr(codigo));
 sql.Open;
 //showmessage(sql.sql.Text);
  if sql.RecordCount > 0 then begin
   existe := true;
   ShortDateFormat := 'yyyy-mm-dd';
   data := sql.fieldbyname('data').AsString;
   ShortDateFormat := 'dd/mm/yyyy';
  end;
 sql.close;
 sql.Free;
end;

end.
